package com.example.gui;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import com.google.android.material.bottomnavigation.BottomNavigationView;

public class ComputerActivity extends AppCompatActivity implements View.OnClickListener{

    private TextView tv_result;
    private String firstNum = "";//第一个操作数

    private String operator = "";//运算符
    private String secondNum = "";//第二个操作数

    private String result = "";//结果

    private String showText = "";//显示器显示

    private ComputerActivity self = this;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_computer);
        tv_result = findViewById(R.id.显示器);

        findViewById(R.id.btn_cancel).setOnClickListener(this);
        findViewById(R.id.btn_divide).setOnClickListener(this);
        findViewById(R.id.btn_mul).setOnClickListener(this);
        findViewById(R.id.btn_clear).setOnClickListener(this);
        findViewById(R.id.btn_seven).setOnClickListener(this);
        findViewById(R.id.btn_eight).setOnClickListener(this);
        findViewById(R.id.btn_nine).setOnClickListener(this);
        findViewById(R.id.btn_add).setOnClickListener(this);
        findViewById(R.id.btn_four).setOnClickListener(this);
        findViewById(R.id.btn_five).setOnClickListener(this);
        findViewById(R.id.btn_six).setOnClickListener(this);
        findViewById(R.id.btn_subtract).setOnClickListener(this);
        findViewById(R.id.btn_one).setOnClickListener(this);
        findViewById(R.id.btn_two).setOnClickListener(this);
        findViewById(R.id.btn_three).setOnClickListener(this);
        findViewById(R.id.btn_sqrt).setOnClickListener(this);
        findViewById(R.id.btn_reciprocal).setOnClickListener(this);
        findViewById(R.id.btn_zero).setOnClickListener(this);
        findViewById(R.id.btn_dot).setOnClickListener(this);
        findViewById(R.id.btn_equal).setOnClickListener(this);

        //找到导航栏
        BottomNavigationView bottomNavigationView = findViewById(R.id.bottomNavigationView);

        bottomNavigationView.getMenu().findItem(R.id.discovery).setChecked(true);

        bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                // 处理每个选项的点击事件
                int itemId = item.getItemId();
                if (itemId == R.id.conversation_list) {
                        // 获取当前显示的 Activity
                            Intent intent = new Intent(self, HomeActivity.class);
                            startActivity(intent);
                        overridePendingTransition(0, 0); // 取消Activity转场动画
                    // 处理对话列表的点击事件，执行相应的页面跳转或页面切换操作
                } else if (itemId == R.id.contact) {
                    String a = "功能未开发";
                    SetMessage(a);
                    // 处理通讯录的点击事件
                } else if (itemId == R.id.setpost) {
                    Intent intent2 = new Intent(self, SetPostActivity.class);
                    startActivity(intent2);
                    // 处理发帖的点击事件
                } else if (itemId == R.id.discovery) {
                    Intent intent2 = new Intent(self, ComputerActivity.class);
                    startActivity(intent2);
                    overridePendingTransition(0, 0); // 取消Activity转场动画
                    // 处理小程序的点击事件
                } else if (itemId == R.id.me) {
                    Intent intent2 = new Intent(self, UserActivity.class);
                    startActivity(intent2);
                    overridePendingTransition(0, 0); // 取消Activity转场动画
                    // 处理我的的点击事件
                }
                return true; // 返回 true 表示消费了该事件，返回 false 则表示未消费
            }
        });


    }

    @Override
    public void onClick(View v) {
        String inputText;

        if(v.getId() == R.id.btn_sqrt){
            inputText = "√";
        }else{
            inputText = ((TextView)v).getText().toString();

        }

        int id = v.getId();
        if (id == R.id.btn_clear) {
            //点击了清除按钮
            clear();
        } else if (id == R.id.btn_cancel) {//点击了取消按钮
        } else if (id == R.id.btn_add || id == R.id.btn_subtract || id == R.id.btn_mul || id == R.id.btn_divide) {
            //点击了加减乘除
            if(operator.isEmpty()){
                operator = inputText;
                refreshText(showText + operator);
            }else{
                //视作点了等于号再加上符号

            }



        } else if (id == R.id.btn_equal) {

            double calculate_result = calculateFour();
            refreshOperate(String.valueOf(calculate_result));
            refreshText(result);
        } else if (id == R.id.btn_sqrt) {
            //点击了开根号按钮
            if(secondNum.equals("")){
                double sqrt_result = Math.sqrt(Double.parseDouble(firstNum));
                refreshOperate(String.valueOf(sqrt_result));
                refreshText(result);
            }

        } else if (id == R.id.btn_reciprocal) {
            //求分数
            if(secondNum.equals("")){
                double reciprocal_result = 1.0/Double.parseDouble(firstNum);
                refreshOperate(String.valueOf(reciprocal_result));
                refreshText(result);
            }
        } else {

            if(result.length() > 0 && operator.equals("")){
                clear();
                result = "";
                operator = "";
            }

            if (operator.equals("")) {
                firstNum = firstNum + inputText;
            } else {
                secondNum = secondNum + inputText;
            }
            if (showText.equals("0") && !inputText.equals(".")) {
                refreshText(inputText);
            } else {
                refreshText(showText + inputText);
            }
        }

    }

    private double calculateFour() {
        switch (operator){
            case "➕":
                return Double.parseDouble(firstNum) + Double.parseDouble(secondNum);
            case "➖":
                return Double.parseDouble(firstNum) - Double.parseDouble(secondNum);
            case "×":
                return Double.parseDouble(firstNum) * Double.parseDouble(secondNum);
            case "➗":
                return Double.parseDouble(firstNum) / Double.parseDouble(secondNum);
        }


        return 0;
    }

    private void clear() {
        refreshText("0");
        refreshOperate("0");
    }

    private void refreshOperate(String new_result){
        result = new_result;
        firstNum = result;
        secondNum = "";
        operator = "";
    }

    private void refreshText(String text){
        showText = text;
        tv_result.setText(showText);
    }

    private void SetMessage(String ms){
        AlertDialog.Builder builder = new AlertDialog.Builder(self);
        builder.setTitle("提示")
                .setMessage(ms)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogInterface.dismiss();
                    }
                })
                .create()
                .show();
    }
}